package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.entity.User;
import com.woniu.mapper.UserMapper;
import com.woniu.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname User2Controller
 * @Description TODO
 * @Date 2025/8/6 11:46
 * @Created by pc
 */
@RestController
@RequestMapping("/user2")
// @CrossOrigin  // 跨域注解
public class User2Controller {

    // rest风格

    @Resource
    private UserService userService;
    @Resource
    private UserMapper userMapper;

    @GetMapping("/{page}/{pageSize}")
    public PageInfo<User> findAll(@PathVariable Integer page, @PathVariable Integer pageSize){
        return userService.findAll(page, pageSize);
    }
    @GetMapping("/{id}")
    public User findById(@PathVariable Integer id){
        User user = userMapper.findById(id);
        return user;
    }

    @PostMapping
    public Map<String,Object> login(@RequestBody User user) throws Exception {
        Map<String, Object> login = userService.login(user);
        return login;
    }
    @DeleteMapping("/{id}")
    public Map<String,Object> del(@PathVariable Integer id){
        Integer result = userMapper.del(id);
        Map<String,Object> map = new HashMap<>();
        if(result > 0){
            map.put("code",200);
            map.put("msg","删除成功");
        }else{
            map.put("code",500);
            map.put("msg","删除失败");
        }
        return map;
    }
    @PostMapping("/add")
    public Map<String,Object> addUser(@RequestBody User user){
        Integer result = userMapper.addUser(user);
        Map<String,Object> map = new HashMap<>();
        if(result > 0){
            map.put("code",200);
            map.put("msg","添加成功");
        }else{
            map.put("code",500);
            map.put("msg","添加失败");
        }
        return map;
    }
}
